#include "ITemp.h"

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int ITemp_Init(struct IntTemp_T ** p){
	struct IntTemp_T * itemp_t = * p;
	char * address = "/var/www/html/ITemp.php";
	int ret = 0;
	DEBUG("--------------%s------------",__func__);
	Led_Init();
	DEBUG("conf address:%s",address);
	if((access(address, F_OK)) == -1){
		DEBUG("without file: /var/www/html/ITemp.php");
		ret = ITemp_File_Init(address);
		if(ret == 1)
			return ret;
		ret = ITemp_File_Get(address, &itemp_t);
		if(ret == 1)
			return ret;

	}else{
		ret = ITemp_File_Get(address, &itemp_t);
		if(ret == 1)
			return ret;

	}
	return ret;
}

void air_control_thread(struct IntTemp_T * p){
	char * address = "/var/www/html/ITemp.php";
	char * note_address = "/home/pi/modules/IT_Note.txt";
	char * note = (char *)malloc(1024);
	struct IntTemp_T * itemp_t = p;
	int ret = 0;
	DEBUG("--------------%s------------",__func__);
	pthread_mutex_lock(&mutex);
	while(!*(itemp_t->isDie)){
		DEBUG("%s begin wait", __func__);
		pthread_cond_wait(&cond, &mutex);
		DEBUG("%s end wait", __func__);
		if(*(itemp_t->isDie)){
			break;
		}
		ret = ITemp_File_Get(address, &itemp_t);
		if(ret != 0)
			DEBUG("itemp conf get error");
		DEBUG("air_temp[%d]", *(itemp_t->air_temp));
		air_control(*(itemp_t->air_temp));
		memset(note, 0 ,1024);
		snprintf(note, 1024, "open the air-con op[%d]", *(itemp_t->air_temp));
		ITemp_File_Write(note_address, note);

		Led_Work(AIR_NORMAL);
	}
	pthread_mutex_unlock(&mutex);
	if(note != NULL)
		free(note);
}

void user_check_thread(struct IntTemp_T * p){
	char * address = "/var/www/html/ITemp.php";
	struct IntTemp_T * itemp_t = p;
	int type = 0;
	int ret = 0;
	DEBUG("--------------%s------------",__func__);
	//pthread_mutex_lock(&mutex);
	while(!*(itemp_t->isDie)){
		ret = ITemp_File_Get(address, &itemp_t);
		if(ret != 0)
			DEBUG("get itemp conf ERROR");
		if(*(itemp_t->air_status) != type){
			type = *(itemp_t->air_status);
			Led_Work(AIR_NORMAL);
		}

	}
	//pthread_mutex_unlock(&mutex);
}

void temp_check_thread(struct IntTemp_T * p){
	char * address = "/var/www/html/ITemp.php";
	char * note_address = "/home/pi/modules/IT_Note.txt";
	char * note = (char *)malloc(1024);
	struct IntTemp_T * itemp_t = p;
	int temp = 0, rh = 0;
	int ret = 0;
	DEBUG("--------------%s------------",__func__);
	while(!*(itemp_t->isDie)){
		get_env_temp(&temp,&rh);
		ret = ITemp_File_Get(address, &itemp_t);
		if(ret != 0)
			DEBUG("get itemp conf ERROR");
		DEBUG("[%s]temp%drh%d  ||  COM[%d]",__TIME__,temp,rh, *(itemp_t->temp));
		ret = ITemp_File_Set(address, "env_temp", temp);
		ret = ITemp_File_Set(address, "env_rh", rh);
		if(*(itemp_t->auto_air)){
			DEBUG("auto control is open");
			/*get_env_temp(&temp,&rh);
			ret = ITemp_File_Get(address, &itemp_t);
			if(ret != 0)
				DEBUG("get itemp conf ERROR");
			DEBUG("[%s]temp%drh%d  ||  COM[%d]",__TIME__,temp,rh, *(itemp_t->temp));
			ret = ITemp_File_Set(address, "env_temp", temp);
			ret = ITemp_File_Set(address, "env_rh", rh);*/
			memset(note, 0 ,1024);
			snprintf(note, 1024,"atuo tyep : temp[%d] rh[%d]", temp, rh);
			ITemp_File_Write(note_address, note);
			if(temp >= *(itemp_t->temp)){
				ret = ITemp_File_Set(address, "OP", OPEN_26);
				if(ret != 0)
					DEBUG("SET op ERROR");
				ret = ITemp_File_Get(address, &itemp_t);
				if(ret != 0)
					DEBUG("get itemp conf ERROR");
				DEBUG("op[%d]", *(itemp_t->air_cont));
				if(*(itemp_t->air_status) == 0){
					DEBUG("AIR HAVE NOT OPEN");
					ITemp_Thr_Wakeup();
					*(itemp_t->air_status) = 1;
					ret = ITemp_File_Set(address, "AIR_STATUS", 1 );
					if(ret != 0)
						DEBUG("SET temp ERROR");
				}else{
					DEBUG("AIR OPENEDA");
				}
			}
			sleep(10);
		}
	}
	if(note != NULL)
		free(note);
}

int ITemp_Thr_Init(struct IntTemp_T ** p){
	char * address = "/var/www/html/ITemp.php";
	struct IntTemp_T * itemp_t = * p;
	pthread_t air_id, temp_id, user_id;
	int air_ret = 0, temp_ret = 0, user_ret = 0;
	*(itemp_t->isDie) = 0;
	*(itemp_t->air_temp) = OPEN_26;
	*(itemp_t->air_status) = 0;
	*(itemp_t->air_cont) = OPEN_26;
	*(itemp_t->temp) = 29;
	DEBUG("--------------%s------------",__func__);
	air_ret = pthread_create(&air_id, NULL, (void *)air_control_thread, itemp_t);
	temp_ret = pthread_create(&temp_id, NULL, (void *)temp_check_thread, itemp_t);
	user_ret = pthread_create(&user_id, NULL, (void *)user_check_thread, itemp_t);
	if((air_ret != 0) || (temp_ret != 0) || (user_ret != 0)){
		DEBUG("TO thread failed");
		return 1;
	}
	pthread_detach(air_id);
	pthread_detach(user_id);
	pthread_detach(temp_id);
	while(!*(itemp_t->isDie)){
		ITemp_File_Get(address, &itemp_t);
	}
	return 0;
}

int ITemp_Thr_Wakeup(){
	DEBUG("%s",__func__);
	pthread_mutex_lock(&mutex);
	pthread_cond_signal(&cond);
	pthread_mutex_unlock(&mutex);
	return 0;
}

int ITemp_Thre_Die(struct IntTemp_T ** p){
	struct IntTemp_T * itemp_t = * p;
	DEBUG("%s",__func__);
	pthread_mutex_lock(&mutex);
	*(itemp_t->isDie) = 1;
	pthread_cond_signal(&cond);
	pthread_mutex_unlock(&mutex);
	return 0;
}

int ITemp_Free(struct IntTemp_T * itemp_t){
	DEBUG("--------------%s------------",__func__);
	if(itemp_t != NULL){
		if(itemp_t->air_cont != NULL)
			free(itemp_t->air_cont);
		if(itemp_t->isDie != NULL)
			free(itemp_t->isDie);
		if(itemp_t->auto_air != NULL)
			free(itemp_t->auto_air);
		free(itemp_t);
	}
	return 0;
}
